/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Inscription.java
 *
 * Created on 18 mars 2009, 09:20:03
 */

package client;

import java.awt.Color;
import java.util.Arrays;

/**
 *
 * @author mailleta
 */
public class Inscription_GUI extends javax.swing.JDialog {

    private java.awt.Frame parent;
    private Client client;
    
    /** Creates new form Inscription */
    public Inscription_GUI(java.awt.Frame parent, boolean modal , Client cl ) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        this.setLocationRelativeTo(this.getParent());
        client = cl;
        this.setVisible(true);
    }

   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        login_label = new javax.swing.JLabel();
        login_textfield = new javax.swing.JTextField();
        password_label = new javax.swing.JLabel();
        annuler_button = new javax.swing.JButton();
        valider_button = new javax.swing.JButton();
        password_confirm_label = new javax.swing.JLabel();
        password_textfield = new javax.swing.JPasswordField();
        password_confirm_textfield = new javax.swing.JPasswordField();
        login_notify_label = new javax.swing.JLabel();
        password_notify_label = new javax.swing.JLabel();
        password_confirm_notify_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inscrivez vous!");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        login_label.setText("Login");

        password_label.setText("Password");

        annuler_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/cancel.png"))); // NOI18N
        annuler_button.setText("Annuler");
        annuler_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                annuler_buttonMousePressed(evt);
            }
        });

        valider_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/icons/tick.png"))); // NOI18N
        valider_button.setText("Inscription");
        valider_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valider_buttonActionPerformed(evt);
            }
        });

        password_confirm_label.setText("Confirmer password");

        login_notify_label.setText(" ");

        password_notify_label.setText(" ");

        password_confirm_notify_label.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(login_label, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(login_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                    .addComponent(login_notify_label, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(password_label, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(password_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                    .addComponent(password_notify_label, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(password_confirm_label, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(password_confirm_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                    .addComponent(password_confirm_notify_label, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(valider_button, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(login_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(login_label))
                .addGap(5, 5, 5)
                .addComponent(login_notify_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password_label)
                    .addComponent(password_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(password_notify_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password_confirm_label)
                    .addComponent(password_confirm_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(password_confirm_notify_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valider_button, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(annuler_button, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void annuler_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_annuler_buttonMousePressed
        this.dispose();

}//GEN-LAST:event_annuler_buttonMousePressed

    private void valider_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valider_buttonActionPerformed
        // TODO add your handling code here:
        String pseudo = login_textfield.getText();
        if(Arrays.equals(password_confirm_textfield.getPassword(),password_textfield.getPassword())) {
            char[] password_chr = password_textfield.getPassword();
            String password = String.valueOf(password_chr);
            char[] password_confirm_chr = password_confirm_textfield.getPassword();
            String password_confirm = String.valueOf(password_chr);
            try {
                if(pseudo.isEmpty()) {
                    login_notify_label.setForeground(Color.red);
                    login_notify_label.setText("Veuillez saisir un login valide.");
                } else if(password.isEmpty()) {
                    password_notify_label.setForeground(Color.red);
                    password_notify_label.setText("Veuillez saisir un password valide.");
                } else if(password_confirm.isEmpty()) {
                    password_notify_label.setForeground(Color.red);
                    password_notify_label.setText("Veuillez saisir un password valide.");
                } else {
                    client.traiterInscriptionChat(pseudo, password);
                    this.dispose();
                }
            } catch (Exception ex) {
                 new TraiteException(ex,"Probleme d'entrées/sorties");
            }
        } else {
            password_notify_label.setForeground(Color.red);
            password_notify_label.setText("Les deux passwords sont différents.");
            password_confirm_notify_label.setForeground(Color.red);
            password_confirm_notify_label.setText("Les deux passwords sont différents.");
        }
    }//GEN-LAST:event_valider_buttonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
/*    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Inscription_GUI dialog = new Inscription_GUI(new javax.swing.JFrame(), true , new Client() );
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton annuler_button;
    private javax.swing.JLabel login_label;
    private javax.swing.JLabel login_notify_label;
    private javax.swing.JTextField login_textfield;
    private javax.swing.JLabel password_confirm_label;
    private javax.swing.JLabel password_confirm_notify_label;
    private javax.swing.JPasswordField password_confirm_textfield;
    private javax.swing.JLabel password_label;
    private javax.swing.JLabel password_notify_label;
    private javax.swing.JPasswordField password_textfield;
    private javax.swing.JButton valider_button;
    // End of variables declaration//GEN-END:variables

}
